<template>
  <div class="rank" ref="rank">
    <Scroll class="toplist" ref="toplist" :click="click">
      <ul>
        <li @click="selectItem(item)" class="item" v-for="(item, index) in topList">
          <div class="icon">
            <img height="100" width="100" v-lazy="item.picUrl">
          </div>
          <ul class="songlist">
            <li class="song" v-for="(song, index) in item.songList">
              <span>{{index + 1}}</span>
              <span>{{song.songname}}-{{song.singername}}</span>
            </li>
          </ul>
        </li>
      </ul>
      <div class="loading-container" v-show="!topList.length">
        <Loading></Loading>
      </div>
    </Scroll>
    <router-view></router-view>
  </div>
</template>

<script>
  import Scroll from 'base/scroll/scroll'
  import {getTopList} from 'api/rank'
  import {ERR_OK} from 'api/config'
  import {playListMixin} from 'common/js/mixin'
  import Loading from 'base/loading/loading'
  import {mapMutations} from 'vuex'

  export default {
    mixins: [
      playListMixin
    ],
    data() {
      return {
        click: true,
        topList: []
      }
    },
    created() {
      this._getTopList()
    },
    methods: {
      handlePlayList(playList) {
        const bottom = playList.length ? `60px` : ``
        this.$refs.rank.style.bottom = bottom
        this.$refs.toplist.refresh()
      },
      _getTopList() {
        getTopList()
          .then(res => {
            if (res.code === ERR_OK) {
              this.topList = res.data.topList
            }
          })
      },
      selectItem(item) {
        this.$router.push({
          path: `/rank/${item.id}`
        })
        this.setTopList(item)
      },
      ...mapMutations({
        setTopList: 'SET_TOP_LIST'
      })
    },
    components: {
      Scroll,
      Loading
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss">
  @import "~common/scss/variable";
  @import "~common/scss/mixin";

  .rank {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    .toplist {
      height: 100%;
      overflow: hidden;
      .item {
        display: flex;
        margin: 0 20px;
        padding-top: 20px;
        height: 100px;
        &:last-child {
          padding-bottom: 20px;
        }
        .icon {
          flex: 0 0 100px;
          width: 100px;
          height: 100px;
        }
        .songlist {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: center;
          padding: 0 20px;
          height: 100px;
          overflow: hidden;
          background: $color-highlight-background;
          color: $color-text-d;
          font-size: $font-size-small;
          box-shadow: 2px 2px 0px #F5387D, 2px 2px 0px #B120FA;
          .song {
            @include no-wrap();
            line-height: 26px;
          }
        }
      }
      .loading-container {
        position: absolute;
        width: 100%;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
</style>
